package cars;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class Customers extends Users {
    private double money;
    private String identity;
    private Vehicle car;
    private int day;

    public Customers() {
    }


    public Customers(String userID, String userPWD, String userName, double money, String identity, Vehicle car, int day) {
        super(userID, userPWD, userName);
        this.money = money;
        this.identity = identity;
        this.car = car;
        this.day = day;
    }

    static void Buff(ArrayList<Vehicle> list, ArrayList<Users> users) throws IOException {
        BufferedWriter bw1 = new BufferedWriter(new FileWriter("AllUsers"));
        for (Users user : users) {
            bw1.write(user.toString());
            bw1.newLine();
        }
        bw1.close();
        BufferedWriter bw2 = new BufferedWriter(new FileWriter("AllCars"));
        for (Vehicle car1 : list) {
            bw2.write(car1.toString());
            bw2.newLine();
        }
        bw2.close();
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    public String toString() {
        return super.toString() + "-" + identity + "-" + money + "-" + car + "-" + day;
    }

    public String getIdentity() {
        return identity;
    }

    public void setIdentity(String identity) {
        this.identity = identity;
    }

    public void ViewCar(ArrayList<Vehicle> list) {
        for (Vehicle vehicle : list) {
            System.out.println(vehicle);
        }
    }

    public void RentCar(ArrayList<Vehicle> list, ArrayList<Users> users) throws IOException {
        ArrayList<String> Brand = new ArrayList<>();
        ArrayList<String> Brand1 = new ArrayList<>();
        ArrayList<String> Brand2 = new ArrayList<>();
        ArrayList<String> Model = new ArrayList<>();
        ArrayList<Integer> Type1 = new ArrayList<>();
        ArrayList<Integer> Type2 = new ArrayList<>();
        if (this.car != null) {
            System.out.println("你已经租过车了，如果你想租别的车辆，请进行更换车辆！！！");
            return;
        }
        System.out.println("1.轿车 2.客车 3.卡车");
        System.out.print("请选择你要租车的种类：");
        String choose = sc.next();
        switch (choose) {
            case "1":
                for (Vehicle vehicle : list) {
                    if (vehicle instanceof Car) {
                        if (!Brand.contains(vehicle.getBrand())) {
                            Brand.add(vehicle.getBrand());
                        }
                    }
                }
                String brand;
                while (true) {
                    for (int i = 0; i < Brand.size(); i++) {
                        System.out.print((i + 1) + "." + Brand.get(i) + " ");
                    }
                    System.out.println();
                    System.out.print("请选择你要租赁的品牌：");
                    brand = sc.next();
                    if (Integer.parseInt(brand) > Brand.size() || Integer.parseInt(brand) <= 0) {
                        System.out.println("输入错误，请重新输入！！");
                    } else {
                        break;
                    }
                }
                for (Vehicle vehicle : list) {
                    if (vehicle instanceof Car && vehicle.getBrand().equals(Brand.get(Integer.parseInt(brand) - 1))) {
                        Model.add(((Car) vehicle).getModel());
                    }
                }
                for (int i = 0; i < Model.size(); i++) {
                    System.out.print((i + 1) + "." + Model.get(i) + " ");
                }
                System.out.println();
                System.out.print("请选择你要租的轿车车型：");
                String type = sc.next();
                if (Integer.parseInt(type) > Model.size() || Integer.parseInt(type) <= 0) {
                    System.out.println("输入错误！");
                    return;
                } else {
                    for (Vehicle vehicle : list) {
                        if (vehicle instanceof Car && ((Car) vehicle).getModel().equals(Model.get(Integer.parseInt(type) - 1)) && vehicle.getBrand().equals(Brand.get(Integer.parseInt(brand) - 1))) {
                            System.out.print("请输入您要租的天数：");
                            int day = sc.nextInt();
                            System.out.println("您分配到的车牌号为：" + vehicle.getVehicleID());
                            System.out.println("您需要支付：" + vehicle.calRent(day));
                            PayCar(list, vehicle.calRent(day), vehicle, users, day);
                            return;
                        }
                    }
                }
                System.out.println("没有该选项！！");
                break;
            case "2":
                for (Vehicle vehicle : list) {
                    if (vehicle instanceof Bus) {
                        if (!Brand1.contains(vehicle.getBrand())) {
                            Brand1.add(vehicle.getBrand());
                        }
                    }
                }
                while (true) {
                    for (int i = 0; i < Brand1.size(); i++) {
                        System.out.print((i + 1) + "." + Brand1.get(i) + " ");
                    }
                    System.out.println();
                    System.out.print("请选择你要租赁的品牌：");
                    brand = sc.next();
                    if (Integer.parseInt(brand) > Brand1.size() || Integer.parseInt(brand) <= 0) {
                        System.out.println("输入错误，请重新输入！！");
                    } else {
                        break;
                    }
                }
                for (Vehicle vehicle : list) {
                    if (vehicle instanceof Bus && vehicle.getBrand().equals(Brand1.get(Integer.parseInt(brand) - 1))) {
                        Type1.add(((Bus) vehicle).getSeat());
                    }
                }
                for (int i = 0; i < Type1.size(); i++) {
                    System.out.print((i + 1) + "." + Type1.get(i) + " ");
                }
                System.out.println();
                System.out.print("请选择想要座位数：");
                int ChooseSeat = sc.nextInt();
                if (ChooseSeat > Type1.size() || ChooseSeat <= 0) {
                    System.out.println("输入错误！");
                    return;
                }
                for (Vehicle vehicle : list) {
                    if (vehicle instanceof Bus && vehicle.getBrand().equals(Brand1.get(Integer.parseInt(brand) - 1)) && ((Bus) vehicle).getSeat() == Type1.get(ChooseSeat - 1)) {
                        System.out.print("请输入您要租的天数：");
                        int day = sc.nextInt();
                        System.out.println("您分配到的车牌号为：" + vehicle.getVehicleID());
                        System.out.println("您需要支付：" + vehicle.calRent(day));
                        PayCar(list, vehicle.calRent(day), vehicle, users, day);
                        return;
                    }
                }
            case "3":
                for (Vehicle vehicle : list) {
                    if (vehicle instanceof Trunk) {
                        if (!Brand2.contains(vehicle.getBrand())) {
                            Brand2.add(vehicle.getBrand());
                        }
                    }
                }
                while (true) {
                    for (int i = 0; i < Brand2.size(); i++) {
                        System.out.print((i + 1) + "." + Brand2.get(i) + " ");
                    }
                    System.out.println();
                    System.out.print("请选择你要租赁的品牌：");
                    brand = sc.next();
                    if (Integer.parseInt(brand) > Brand2.size() || Integer.parseInt(brand) <= 0) {
                        System.out.println("输入错误，请重新输入！！");
                    } else {
                        break;
                    }
                }
                for (Vehicle vehicle : list) {
                    if (vehicle instanceof Trunk && vehicle.getBrand().equals(Brand2.get(Integer.parseInt(brand) - 1))) {
                        Type2.add(((Trunk) vehicle).getTonnage());
                    }
                }
                for (int i = 0; i < Type2.size(); i++) {
                    System.out.print((i + 1) + "." + Type2.get(i) + " ");
                }
                System.out.println();
                System.out.print("请选择想要载重量：：");
                int ChooseTon = sc.nextInt();
                if (ChooseTon > Type2.size() || ChooseTon <= 0) {
                    System.out.println("输入错误！");
                    return;
                }
                for (Vehicle vehicle : list) {
                    if (vehicle instanceof Trunk && vehicle.getBrand().equals(Brand2.get(Integer.parseInt(brand) - 1)) && ((Trunk) vehicle).getTonnage() == Type2.get(ChooseTon - 1)) {
                        System.out.print("请输入您要租的天数：");
                        int day = sc.nextInt();
                        System.out.println("您分配到的车牌号为：" + vehicle.getVehicleID());
                        System.out.println("您需要支付：" + vehicle.calRent(day));
                        PayCar(list, vehicle.calRent(day), vehicle, users, day);
                        return;
                    }
                }
        }
    }


    public void ChangeCar(ArrayList<Vehicle> list, ArrayList<Users> users) throws IOException {
        if (this.car == null) {
            System.out.println("您还未租借任何车辆，无法进行更换车辆!!");
            return;
        }
        while (true) {
            System.out.println("换车将扣除100元手续费！！");
            System.out.println("是否确认更换：");
            System.out.println("是   否");
            String choose = sc.next();
            if (choose.equals("否")) {
                return;
            } else if (choose.equals("是")) {
                list.add(this.car);
                money = money + car.calRent(day) - 100;
                this.car = null;
                this.day = 0;
                BufferInCar(list, users);
                RentCar(list, users);
                System.out.println("更换车辆成功！！");
                return;
            } else {
                System.out.println("输入错误，请重新输入");
            }
        }
    }

    public void PayCar(ArrayList<Vehicle> list, double fee, Vehicle vehicle, ArrayList<Users> users, int day) throws IOException {
        System.out.println("你现在剩余：" + this.money + "元");
        if (this.money < fee) {
            System.out.println("你剩余的钱不够支付，请前往管理员处进行充值");
            return;
        }
        System.out.println("是否确认支付");
        System.out.println("是  否");
        String bollen = sc.next();
        while (true) {
            if (bollen.equals("否")) {
                return;
            } else if (bollen.equals("是")) {
                System.out.println("支付成功！！");
                this.money -= fee;
                System.out.println("你现在还剩余" + this.money + "元!!");
                System.out.println("车牌号为：" + vehicle.getVehicleID() + "的车被您租走了！");
                this.day = day;
                list.remove(vehicle);
                this.car = vehicle;
                BufferInCar(list, users);
                return;
            } else {
                System.out.println("无效字符，请重新输入！！");
            }
        }
    }

    public void BufferInCar(ArrayList<Vehicle> list, ArrayList<Users> users) throws IOException {
        Buff(list, users);
    }

    public void AddVIP(ArrayList<Vehicle>list, ArrayList<Users> users) throws IOException {
        System.out.println("成为会员需要缴费5000元，是否确认");
        while (true) {
            System.out.println("是  否");
            String choose = sc.next();
            if (choose.equals("是")) {
                if (money < 5000) {
                    System.out.println("你剩余的金额不足，请前往管理员处进行充值");
                    return;
                }
                money -= 5000;
                identity = "vip";
                break;
            } else if (choose.equals("否")) {
                return;
            } else {
                System.out.println("错误的输入，请重新输入!!!");
            }
        }
        BufferInCar(list, users);
        System.out.println("恭喜你成为会员，请重新登录使用最新功能！");
    }
}
